import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import store from "@/store";

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: "/home"
  },
  {
    path: '/home',
    name: 'Home',
    component: Home
  },
  // {
  //   path: '/about',
  //   name: 'About',
  //   // route level code-splitting
  //   // this generates a separate chunk (about.[hash].js) for this route
  //   // which is lazy-loaded when the route is visited.
  //   component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
  // }
  , {
    path: '/free',
    component: () => import(/* webpackChunkName: "free" */ '../views/Free.vue')
  }, {
    path: '/goods',
    component: () => import(/* webpackChunkName: "goods" */ '../views/Goods.vue')
  }, {
    path: '/order',
    component: () => import(/* webpackChunkName: "order" */ '../views/Order.vue')
  }, {
    redirect: "/user/cart",
    path: '/user',
    component: () => import(/* webpackChunkName: "user" */ '../views/User.vue'),
    children: [//user下面的子路由
      {//user里面的购物车
        // path:'/cart'   绝对路径
        path: '/user/cart',//相对路径
        component: () => import(/* webpackChunkName: "cart" */ '../components/user/Cart.vue'),
      }
    ]
  }, {
    path: '/details',
    component: () => import(/* webpackChunkName: "details" */ '../views/Details.vue')
  }, {
    path: '*',
    name: "Error",
    component: () => import(/* webpackChunkName: "error" */ '../views/Error.vue')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function (location) {
  return originalPush.call(this, location).catch(err => { })
};

//全局导航守卫   全局守卫
// router.beforeEach((to, from, next) => {
//   //跳转到一个路由之前,都会执行这里的代码

//   //to   要去的那个路由的对象
//   //from   从哪个路径跳转到那个路由的对象
//   // console.log(`从${from.path}跳转到${to.path}`);

//   //业务需求:没登录之前不能进去个人中心
//   //先判断用户是不是进去个人中心
//   if (to.path === "/user") {
//     //再判断有没有登录
//     let token = localStorage.getItem("x-auth-token")
//     if (token) {
//       //登录了就放行
//       next()
//     } else {
//       //没有登录,就提示用户
//       store.dispatch("showToast/asyncChanIsShowToast", {
//         msg: "请先登录!",
//         type: "warning"
//       })
//     }
//     return //需要写return 防止上面的next执行完,又再执行下面的next
//   }
//   next()
// })
export default router
